package com.xd.pre.modules.tpshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.pre.modules.tpshop.domain.TpGoodsCategory;
import com.xd.pre.modules.tpshop.domain.TpOrder;
import com.xd.pre.modules.tpshop.domain.TpSpecGoodsPrice;
import com.xd.pre.modules.tpshop.mapper.TpSpecGoodsPriceMapper;
import com.xd.pre.modules.tpshop.service.ITpSpecGoodsPriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@Service
public class TpSpecGoodsPriceServiceImpl extends ServiceImpl<TpSpecGoodsPriceMapper, TpSpecGoodsPrice> implements ITpSpecGoodsPriceService {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<Integer> find_goodsId_by_keyLike(String keyPart){
        //	 SELECT * FROM `tp_spec_goods_price` tsgp WHERE tsgp.`key` LIKE '%\_42\_%'
        //		OR  tsgp.`key` LIKE '42\_%'
        //		OR  tsgp.`key` LIKE '%\_42'

        String hql="select DISTINCT goods_id from tp_spec_goods_price tsgp " +
        //							 '%      \_	    42	     \_		   %'
                "where tsgp.key like '%"+  "\\_"+ keyPart +"\\_"     +"%' "
            +	   "or tsgp.key like '" +		   keyPart +"\\_"    +"%' "
            +	   "or tsgp.key like '%"+  "\\_"+ keyPart 	 	     +"'  ";

        String hql_test="select key " +
                "from TpSpecGoodsPrice where    key like '" +		  keyPart +"\\_"    +"%' " ;
        List<Integer> result = jdbcTemplate
                .query(hql, new Object[]{}, new RowMapper<Integer>() {
                    @Override
                    public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return result2po(rs);
                    }
                });

        return result;
    }

    @Override
    public TpSpecGoodsPrice find_SpecGoodsPrice_byGoodsId_key(int goods_id, String goods_spec_1) {
        LambdaQueryWrapper<TpSpecGoodsPrice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TpSpecGoodsPrice::getGoodsId, goods_id);
        queryWrapper.eq(TpSpecGoodsPrice::getKey, goods_spec_1);
        TpSpecGoodsPrice one = getOne(queryWrapper);
        return one;
    }

    @Override
    public TpSpecGoodsPrice find_SpecGoodsPrice_by_primaryKey(Integer middleSpecId) {
        return getById(middleSpecId);
    }

    private Integer result2po(ResultSet result) throws SQLException {
//        TpSpecGoodsPrice moneyPO = new TpSpecGoodsPrice();
//        moneyPO.setMiddleSpecId(result.getInt("middle_spec_id"));
//        moneyPO.setGoodsId(result.getInt("goods_id"));
//        moneyPO.setKey(result.getString("key"));
//        moneyPO.setKeyName(result.getString("key_name"));
//        moneyPO.setPrice(result.getBigDecimal("price"));
//        moneyPO.setStoreCount(result.getInt("store_count"));
//        moneyPO.setBarCode(result.getString("bar_code"));
//        moneyPO.setSku(result.getString("sku"));
        Integer goods_id = result.getInt("goods_id");
        return goods_id;
    }
}
